package com.swt.gap.controller;


import com.swt.gap.common.PageResult;
import com.swt.gap.model.vo.resourcecenter.DataListVO;
import com.swt.gap.model.vo.resourcecenter.DataPassword;
import com.swt.gap.model.vo.resourcecenter.DataResourceStatistics;
import com.swt.gap.service.DataResourceService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "202 数据资源 新")
@RestController
@RequestMapping("/data-resource")
public class DataResourceController {

    private final DataResourceService dataResourceService;

    public DataResourceController(DataResourceService dataResourceService) {
        this.dataResourceService = dataResourceService;
    }
    @GetMapping
    @ApiOperation(value = "获取数据资源列表", notes = "获取数据资源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", dataType = "string",paramType = "query", value = "数据名称"),
            @ApiImplicitParam(name = "fileClassId", dataType = "Integer",paramType = "query", value = "分类id"),
            @ApiImplicitParam(name = "pageNum", dataType = "int", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", dataType = "int", value = "每页的条数"),
            @ApiImplicitParam(name = "isOwnCreated", dataType = "bit", value = "是否自己创建的，不填则不限"),
            @ApiImplicitParam(name = "hasPassword", dataType = "bit", value = "是否有密码的，不填则不限"),
    })
    @ApiResponse(code = 200, message = "获取成果", response = PageResult.class)
    public ResponseEntity<PageResult<DataListVO>> listDataResource(@RequestParam(required = false) String keyword,
                                                                   @RequestParam(required = false) Integer fileClassId,
                                                                   @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                                   @RequestParam(value = "isOwnCreated", required = false) Boolean isOwnCreated,
                                                                   @RequestParam(value = "hasPassword", required = false) Boolean hasPassword){
        return ResponseEntity.ok(dataResourceService.listDataResources(keyword, fileClassId, pageNum, pageSize, isOwnCreated, hasPassword));
    }

    @PostMapping("/password")
    @ApiOperation(value = "设置或者更新数据资源的密码", notes = "设置或者更新数据资源的密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataPassword", dataType = "DataPassword", paramType = "body")
    })
    @ApiResponse(code = 200, message = "ok", response = String.class)
    public ResponseEntity<String> addOrUpdateDataPassword(@RequestBody DataPassword dataPassword) {
        Integer userId = UserLoginUtils.getUserId();
        for (Integer fileId : dataPassword.getFileIds()) {
            if (!dataResourceService.hasWritePermission(userId, fileId)) {
                return ResponseEntity.status(HttpStatus.FORBIDDEN).body("无操作权限");
            }
        }
        return ResponseEntity.ok(dataResourceService.updateDataPassword(dataPassword).toString());
    }

    @DeleteMapping("/password")
    @ApiOperation(value = "删除数据资源的密码", notes = "删除数据资源的密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileIds", dataType = "array", paramType = "query")
    })
    @ApiResponse(code = 200, message = "ok", response = String.class)
    public ResponseEntity<String> deleteDataPassword(@RequestParam List<Integer> fileIds) {
        Integer userId = UserLoginUtils.getUserId();
        for (Integer fileId : fileIds) {
            if (!dataResourceService.hasWritePermission(userId, fileId)) {
                return ResponseEntity.status(HttpStatus.FORBIDDEN).body("无操作权限");
            }
        }
        DataPassword dataPassword = new DataPassword();
        dataPassword.setFileIds(fileIds);
        return ResponseEntity.ok(dataResourceService.updateDataPassword(dataPassword).toString());
    }

    @GetMapping("/read-permission")
    @ApiOperation(value = "获取读权限", notes = "获取读权限" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", dataType = "int", paramType = "query", value = "fileId"),
            @ApiImplicitParam(name = "password", dataType = "string", paramType = "query", value = "密码")
    })
    @ApiResponse(code = 200, message = "ok", response = Boolean.class)
    public ResponseEntity<Boolean> getReadPerm(@RequestParam Integer fileId, @RequestParam String password){
        return ResponseEntity.ok(dataResourceService.getReadPerm(fileId, password));
    }

    @GetMapping("/statistics")
    @ApiOperation(value = "资源中心 某个类别下的统计", notes = "资源中心 某个类别下的统计" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileClassId", dataType = "Integer",paramType = "query", value = "分类id"),
    })
    @ApiResponse(code = 200, message = "ok", response = DataResourceStatistics.class)
    public ResponseEntity<DataResourceStatistics> getStatistics(@RequestParam Integer fileClassId){
        return ResponseEntity.ok(dataResourceService.getStatistics(fileClassId));
    }
}
